﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.UI.BaseDataSvc;
using YY.Common;
using System.Diagnostics;

namespace YY.UI
{
    public partial class LoginFrm : BaseFrm
    {
        public LoginFrm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string code = this.txtBoxCode.Text.Trim();
            string pwd = this.txtBoxPwd.Text;
            if (string.IsNullOrEmpty(code))
            {
                MessageBox.Show("请输入用户代码！");
                this.txtBoxCode.Focus();
                return;
            }

            LoginResult lr = this.BaseDataProxy.CheckLogin(code, pwd);
            if (lr != null)
            {
                if (lr.ErrorMsg == string.Empty)
                {
                    SysLoginUserInfo.UserId = lr.User.id;
                    SysLoginUserInfo.UserName = lr.User.userName;
                    SysLoginUserInfo.UserCode = lr.User.code;
                    SysLoginUserInfo.IPs = Common.GetIP();
                    SysLoginUserInfo.Macs = Common.GetMac();
                    SysLoginUserInfo.LoginTime = DateTime.Now;
                    SysLoginUserInfo.Role = lr.User.role;
                    SysLoginUserInfo.IsAdministrator = lr.User.role == (int)EnumCommon.SysRoleType.管理员;

                    this.DialogResult = DialogResult.OK;
                }
                else
                    MessageBox.Show(lr.ErrorMsg);
            }
            else
                MessageBox.Show("连接服务器失败！");
        }

        private void LoginFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
